
import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import 'state.dart';

class MyCouponLogic extends GetxController {
  final state = MyCouponState();

  @override
  void onInit() {
    super.onInit();

    state.categoryIndex = 0;
  }

  @override
  void onReady() {
    super.onReady();

    var params = {
      'use_platform': 2,
      'access_token':  SpUtil.getString('token'),
      'status': '1',
      'page': state.page,
      'rows': 10,
      'usr_num_id': SpUtil.getInt('empeNumId'),
    };
    Api.availableCouponsList(params).listen((event) {
      state.availableCouponsIndex = event['total'];
      List list = event['usr_vouchers'];
      if (list != null && list.length > 0) {
        list.forEach((value) {
          var startDate = DateTime.parse(value['sdate']);
          var endDate = DateTime.parse(value['edate']);
          value['disable'] = (DateTime.now().isBefore(startDate)) ||
              (DateTime.now().isAfter(endDate));
        });
        state.availableCoupons = list;
      } else {
        state.availableCoupons = [];
      }
      update();
    });

    var params1 = {
      'access_token':  SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'page': state.page,
      'rows': 10,
    };
    //查询失效卡券
    Api.invalidCouponsList(params1).listen((event) {
      state.invalidCouponsIndex = event['total'];
      List list = event['user_card_coupons'];
      if (list != null && list.length > 0) {
        list.forEach((value) {
          var startDate = DateTime.parse(value['sdate']);
          var endDate = DateTime.parse(value['edate']);
          value['disable'] = (DateTime.now().isBefore(startDate)) ||
              (DateTime.now().isAfter(endDate));
        });
        state.invalidCoupons = list;
      } else {
        state.invalidCoupons = [];
      }
      update();
    });
  }
  // 下拉刷新
  void refreshData() {
    state.page = 1;
    if (state.categoryIndex == 0) {
      state.availableCoupons = [];
      var params = {
        'use_platform': 2,
        'access_token':  SpUtil.getString('token'),
        'status': '1',
        'page': state.page,
        'rows': 10,
        'usr_num_id': SpUtil.getInt('empeNumId'),
      };
      Api.availableCouponsList(params).listen((event) {
        state.availableCouponsIndex = event['total']; // 总数量
        List list = event['usr_vouchers'];
        if (list != null) {
          list.forEach((value) {
            var startDate = DateTime.parse(value['sdate']);
            var endDate = DateTime.parse(value['edate']);
            value['disable'] = (DateTime.now().isBefore(startDate)) ||
                (DateTime.now().isAfter(endDate));
          });
          state.availableCoupons = list; // 数据列表
        } else {
          state.availableCoupons = [];
        }
        state.availableCouponsController.refreshCompleted();
        update();
      });
    } else {
      state.invalidCoupons = [];
      var params1 = {
        'access_token':  SpUtil.getString('token'),
        'usr_num_id': SpUtil.getInt('empeNumId'),
        'page': state.page,
        'rows': 10,
      };
      //查询失效卡券
      Api.invalidCouponsList(params1).listen((event) {
        state.invalidCouponsIndex = event['total']; // 总数量
        List list = event['user_card_coupons'];
        if (list != null) {
          list.forEach((value) {
            var startDate = DateTime.parse(value['sdate']);
            var endDate = DateTime.parse(value['edate']);
            value['disable'] = (DateTime.now().isBefore(startDate)) ||
                (DateTime.now().isAfter(endDate));
          });
          state.invalidCoupons = list; // 数据列表
        } else {
          state.invalidCoupons = [];
        }
        state.availableCouponsController.refreshCompleted();
        // state.invalidCouponsController.refreshCompleted();
        update();
      });
    }
  }
  // 上拉加载更多
  void loadMoreData() {
    state.page = state.page + 1;
    if (state.categoryIndex == 0) {
      var params = {
        'use_platform': 2,
        'access_token':  SpUtil.getString('token'),
        'status': '1',
        'page': state.page,
        'rows': 10,
        'usr_num_id': SpUtil.getInt('empeNumId'),
      };
      Api.availableCouponsList(params).listen((event) {
        state.availableCouponsIndex = event['total'];
        List list = state.availableCoupons;
        if (event['usr_vouchers'].length > 0) {
          list.addAll(event['usr_vouchers']);
          list.forEach((value) {
            var startDate = DateTime.parse(value['sdate']);
            var endDate = DateTime.parse(value['edate']);
            value['disable'] = (DateTime.now().isBefore(startDate)) ||
                (DateTime.now().isAfter(endDate));
          });
          state.availableCoupons = list;
          state.availableCouponsController.loadComplete();
        } else {
          state.page = state.page - 1;
          state.availableCouponsController.loadComplete();
        }
        update();
      });
    } else {
      var params1 = {
        'access_token':  SpUtil.getString('token'),
        'usr_num_id': SpUtil.getInt('empeNumId'),
        'page': state.page,
        'rows': 10,
      };
      //查询失效卡券
      Api.invalidCouponsList(params1).listen((event) {
        state.invalidCouponsIndex = event['total'];
        List list = state.invalidCoupons;
        if (event['user_card_coupons'].length > 0) {
          list.addAll(event['user_card_coupons']);
          list.forEach((value) {
            var startDate = DateTime.parse(value['sdate']);
            var endDate = DateTime.parse(value['edate']);
            value['disable'] = (DateTime.now().isBefore(startDate)) ||
                (DateTime.now().isAfter(endDate));
          });
          state.invalidCoupons = list;
          state.availableCouponsController.loadComplete();
          // state.invalidCouponsController.loadComplete();
        } else {
          state.page = state.page - 1;
          state.availableCouponsController.loadComplete();
          // state.invalidCouponsController.loadComplete();
        }
        update();
      });
    }
  }

  void onChooseCategory(int index) {
    state.categoryIndex = index;
    state.page = 1;
    if (state.categoryIndex == 0) {
      state.availableCoupons = [];
      var params = {
        'use_platform': 2,
        'access_token':  SpUtil.getString('token'),
        'status': '1',
        'page': state.page,
        'rows': 10,
        'usr_num_id': SpUtil.getInt('empeNumId'),
      };
      Api.availableCouponsList(params).listen((event) {
        state.availableCouponsIndex = event['total'];
        List list = event['usr_vouchers'];
        if (list != null && list.length > 0) {
          list.forEach((value) {
            var startDate = DateTime.parse(value['sdate']);
            var endDate = DateTime.parse(value['edate']);
            value['disable'] = (DateTime.now().isBefore(startDate)) ||
                (DateTime.now().isAfter(endDate));
          });
          state.availableCoupons = list;
        } else {
          state.availableCoupons = [];
        }
        update();
      });
    } else {
      state.invalidCoupons = [];
      var params1 = {
        'access_token':  SpUtil.getString('token'),
        'usr_num_id': SpUtil.getInt('empeNumId'),
        'page': state.page,
        'rows': 10,
      };
      //查询失效卡券
      Api.invalidCouponsList(params1).listen((event) {
        state.invalidCouponsIndex = event['total'];
        List list = event['user_card_coupons'];
        if (list != null && list.length > 0) {
          list.forEach((value) {
            var startDate = DateTime.parse(value['sdate']);
            var endDate = DateTime.parse(value['edate']);
            value['disable'] = (DateTime.now().isBefore(startDate)) ||
                (DateTime.now().isAfter(endDate));
          });
          state.invalidCoupons = list;
        } else {
          state.invalidCoupons = [];
        }
        update();
      });
    }
  }

  // 优惠券赠送
  void givingCoupon({
    String qbarcode,
    String phone,
    int index,
  }) {

    var params = {
      "use_platform": 2,
      "access_token": SpUtil.getString('token'),
      "qbarcode": qbarcode,//券号
      "mobile_phone": phone, //转赠手机号(他人的)
      "donate_usr_num_id": SpUtil.getInt('empeNumId'),//转赠人
      "relation": "转赠", //死值
      "qsource": 9 //死值
    };
    Api.givingCoupon(params).listen((event) {
      print('优惠券赠送 结果返回数据  event = $event');
      if (event['code'] == 0) {
        state.availableCoupons.removeAt(index);
        state.availableCouponsChoose = -1;
        state.availableCouponsIndex = state.availableCouponsIndex - 1;
      }
      toast(event['message'].toString());
      update();
    });
  }

  @override
  void dispose() {
    super.dispose();
    state.availableCouponsController.dispose();
    // state.invalidCouponsController.dispose();
    update();
  }
}